package com.dykj.fireFighting.web.mq;

import com.alibaba.fastjson.JSON;
import com.dykj.fireFighting.api.elasticsearch.devHisZc.entity.EsDevHisZc;
import com.dykj.fireFighting.api.elasticsearch.devHisZc.service.EsDevHisZcService;
import com.dykj.fireFighting.common.starter.redisson.service.RedisService;
import com.dykj.fireFighting.web.entity.DevHisZc;
import com.dykj.fireFighting.web.message.InsertDevHisZcMessage;
import com.dykj.fireFighting.web.service.DevHisZcService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;

/**
 * @author zhengbo
 */
@Service
@RocketMQMessageListener(
        topic = InsertDevHisZcMessage.TOPIC,
        consumerGroup = "dy-consumer-group-" + InsertDevHisZcMessage.TOPIC
)
public class InsertDevHisZcConsumer implements RocketMQListener<InsertDevHisZcMessage> {
    public static final Logger LOGGER = LoggerFactory.getLogger(InsertDevHisZcConsumer.class);
    @Autowired
    private RedisService redisService;
    @Autowired
    private DevHisZcService devHisZcService;
    @DubboReference
    private EsDevHisZcService esDevHisZcService;
    @Override
    public void onMessage(InsertDevHisZcMessage insertDevHisZcMessage) {
        DevHisZc devHisZc =  insertDevHisZcMessage.getDevHisZc();
        devHisZc.setStatus("0");
        LOGGER.info("===========插入devHis数据到mysql==============");
        devHisZcService.insertDevHisZc(devHisZc);
        LOGGER.info("===========插入EsDevHis数据到ES==============");
        EsDevHisZc esDevHisZc = new EsDevHisZc();
        BeanUtils.copyProperties(devHisZc, esDevHisZc);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        esDevHisZc.setUpTime(sdf.format(devHisZc.getUpTime()));
        esDevHisZc.setZtfssj(sdf.format(devHisZc.getZtfssj()));
        esDevHisZc = esDevHisZcService.importAll(esDevHisZc);
        LOGGER.info("===========插入EsDevHis数据到ES:=============" + JSON.toJSONString(esDevHisZc));

        redisService.set(devHisZc.getId(), JSON.toJSONString(devHisZc), 3600L);
    }
}
